package guoanmaker.operator.business.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorRefuselist;
import guoanmaker.operator.business.model.OperatorRole;
import guoanmaker.operator.business.model.OperatorUser;
import guoanmaker.operator.business.model.ProvideCommodity;
import guoanmaker.operator.business.model.ReturnResult;
import guoanmaker.operator.business.repository.OperatorRefuselistRepository;
import guoanmaker.operator.business.repository.OperatorUserRepository;
import guoanmaker.operator.business.repository.ProvideCommodityRepository;
import guoanmaker.operator.helper.common.util.StringUtil;

@Service
public class OperatorRefuselistService {
	
	@Autowired
	private OperatorRefuselistRepository   operatorRefuselistRepository;
	
	@Autowired
	private OperatorUserRepository  operatorUserRepository;
	@Autowired  
	private  HttpServletRequest request;
	
	
	@Autowired
	private ProvideCommodityRepository  provideCommodityRepository;
	//商户
	public  Page<OperatorRefuselist>   queryAllrefuseListpage(Integer page,Integer size){
		 Pageable pageable = new PageRequest(page, size, Sort.Direction.DESC, "id");  
		 Page<OperatorRefuselist>  list = operatorRefuselistRepository.findAll(new Specification<OperatorRefuselist>(){  
	            @Override  
	            public Predicate toPredicate(Root<OperatorRefuselist> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {  
	            	Predicate p2 = criteriaBuilder.equal(root.get("attribution").as(String.class), "1");  
	                query.where(criteriaBuilder.and(p2));  
	                return query.getRestriction();  
	            }  
	        },pageable);  
		return list;
	}
	//商品
	public  Page<OperatorRefuselist>   queryrefuseCommListpage(Integer page,Integer size){
		Pageable pageable = new PageRequest(page, size, Sort.Direction.DESC, "id");  
		Page<OperatorRefuselist>  list = operatorRefuselistRepository.findAll(new Specification<OperatorRefuselist>(){  
			@Override  
			public Predicate toPredicate(Root<OperatorRefuselist> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {  
				Predicate p2 = criteriaBuilder.equal(root.get("attribution").as(String.class), "2");  
				query.where(criteriaBuilder.and(p2));  
				return query.getRestriction();  
			}  
		},pageable);  
		return list;
	}
	//下架
	public  Page<OperatorRefuselist>   queryrefuseShelvesListpage(Integer page,Integer size){
		Pageable pageable = new PageRequest(page, size, Sort.Direction.DESC, "id");  
		Page<OperatorRefuselist>  list = operatorRefuselistRepository.findAll(new Specification<OperatorRefuselist>(){  
			@Override  
			public Predicate toPredicate(Root<OperatorRefuselist> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {  
				Predicate p2 = criteriaBuilder.equal(root.get("attribution").as(String.class), "4");  
				query.where(criteriaBuilder.and(p2));  
				return query.getRestriction();  
			}  
		},pageable);  
		return list;
	}
	//订单关闭原因
	public  Page<OperatorRefuselist>   querycloseorderListpage(Integer page,Integer size){
		Pageable pageable = new PageRequest(page, size, Sort.Direction.DESC, "id");  
		Page<OperatorRefuselist>  list = operatorRefuselistRepository.findAll(new Specification<OperatorRefuselist>(){  
			@Override  
			public Predicate toPredicate(Root<OperatorRefuselist> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {  
				Predicate p2 = criteriaBuilder.equal(root.get("attribution").as(String.class), "3");  
				query.where(criteriaBuilder.and(p2));  
				return query.getRestriction();  
			}  
		},pageable);  
		return list;
	}
	//商户
	@Transactional
	public ReturnResult   addrefuseList(String refuseName,String userid){
		ReturnResult  returnResult=new ReturnResult();
		if(refuseName!=null&&!refuseName.equals("")){
			List<OperatorRefuselist> list= operatorRefuselistRepository.selectByCategoryName(refuseName);
			if(list.size()>0){
				returnResult.setKey("error");
				returnResult.setValue("名称不可重复");
			}else{
				OperatorRefuselist  operatorRefuselist=new OperatorRefuselist();
				operatorRefuselist.setCategoryName(refuseName);
				OperatorUser operatorUser=operatorUserRepository.findOne(userid);
				if(operatorUser!=null){
					operatorRefuselist.setCreateUser(operatorUser.getUsername());	
				}
				operatorRefuselist.setAttribution(1);
				operatorRefuselist.setCreateDate(new Date());
				operatorRefuselistRepository.save(operatorRefuselist);
				returnResult.setKey("success");
				returnResult.setValue("保存成功");
				
			}
		}else{
			returnResult.setKey("error");
			returnResult.setValue("名称不可为空");
		}
		return  returnResult;
	}
	//商品
	@Transactional
	public ReturnResult   addCommrefuse(String refuseName,String userid){
		ReturnResult  returnResult=new ReturnResult();
		if(refuseName!=null&&!refuseName.equals("")){
			List<OperatorRefuselist> list= operatorRefuselistRepository.selectByCommCategoryName(refuseName);
			if(list.size()>0){
				returnResult.setKey("error");
				returnResult.setValue("名称不可重复");
			}else{
				OperatorRefuselist  operatorRefuselist=new OperatorRefuselist();
				operatorRefuselist.setCategoryName(refuseName);
				OperatorUser operatorUser=operatorUserRepository.findOne(userid);
				if(operatorUser!=null){
					operatorRefuselist.setCreateUser(operatorUser.getUsername());	
				}
				operatorRefuselist.setAttribution(2);
				operatorRefuselist.setCreateDate(new Date());
				operatorRefuselistRepository.save(operatorRefuselist);
				returnResult.setKey("success");
				returnResult.setValue("保存成功");
				
			}
		}else{
			returnResult.setKey("error");
			returnResult.setValue("名称不可为空");
		}
		return  returnResult;
	}
	@Transactional
	public ReturnResult   addShelves(String refuseName,String userid){
		ReturnResult  returnResult=new ReturnResult();
		if(refuseName!=null&&!refuseName.equals("")){
			List<OperatorRefuselist> list= operatorRefuselistRepository.selectByShelvesCategoryName(refuseName);
			if(list.size()>0){
				returnResult.setKey("error");
				returnResult.setValue("名称不可重复");
			}else{
				OperatorRefuselist  operatorRefuselist=new OperatorRefuselist();
				operatorRefuselist.setCategoryName(refuseName);
				OperatorUser operatorUser=operatorUserRepository.findOne(userid);
				if(operatorUser!=null){
					operatorRefuselist.setCreateUser(operatorUser.getUsername());	
				}
				operatorRefuselist.setAttribution(4);
				operatorRefuselist.setCreateDate(new Date());
				operatorRefuselistRepository.save(operatorRefuselist);
				returnResult.setKey("success");
				returnResult.setValue("保存成功");
			}
		}else{
			returnResult.setKey("error");
			returnResult.setValue("名称不可为空");
		}
		return  returnResult;
	}
	@Transactional
	public ReturnResult   addCloseOrder(String refuseName,String userid){
		ReturnResult  returnResult=new ReturnResult();
		if(refuseName!=null&&!refuseName.equals("")){
			List<OperatorRefuselist> list= operatorRefuselistRepository.selectByOder(refuseName);
			if(list.size()>0){
				returnResult.setKey("error");
				returnResult.setValue("名称不可重复");
			}else{
				OperatorRefuselist  operatorRefuselist=new OperatorRefuselist();
				operatorRefuselist.setCategoryName(refuseName);
				OperatorUser operatorUser=operatorUserRepository.findOne(userid);
				if(operatorUser!=null){
					operatorRefuselist.setCreateUser(operatorUser.getUsername());	
				}
				operatorRefuselist.setAttribution(3);
				operatorRefuselist.setCreateDate(new Date());
				operatorRefuselistRepository.save(operatorRefuselist);
				returnResult.setKey("success");
				returnResult.setValue("保存成功");
			}
		}else{
			returnResult.setKey("error");
			returnResult.setValue("名称不可为空");
		}
		return  returnResult;
	}
	
	@Transactional
	public   ReturnResult   editrefuseList(String id,String refuseName,String userid){
		ReturnResult  returnResult=new ReturnResult();
		if(refuseName!=null&&!refuseName.equals("")){
			List<OperatorRefuselist> list= operatorRefuselistRepository.selectByCategoryName(refuseName);
			if(list.size()>0){
				if(list.get(0).getCategoryName().equals(refuseName)){
					returnResult.setKey("success");
					returnResult.setValue("编辑成功");
				}else{
					returnResult.setKey("error");
					returnResult.setValue("名称不可重复");
				}
			}else{
				OperatorRefuselist  operatorRefuselist=operatorRefuselistRepository.findOne(id);
				operatorRefuselist.setCategoryName(refuseName);
				OperatorUser operatorUser=operatorUserRepository.findOne(userid);
				if(operatorUser!=null){
					operatorRefuselist.setCreateUser(operatorUser.getUsername());	
				}
				operatorRefuselist.setCreateDate(new Date());
				operatorRefuselistRepository.save(operatorRefuselist);
				returnResult.setKey("success");
				returnResult.setValue("编辑成功");
				
			}
		}else{
			returnResult.setKey("error");
			returnResult.setValue("名称不可为空");
		}
		
		return  returnResult;
	}
	@Transactional
	public   ReturnResult   editCommrefuse(String id,String refuseName,String userid){
		ReturnResult  returnResult=new ReturnResult();
		if(refuseName!=null&&!refuseName.equals("")){
			List<OperatorRefuselist> list= operatorRefuselistRepository.selectByCommCategoryName(refuseName);
			if(list.size()>0){
				if(list.get(0).getCategoryName().equals(refuseName)){
					returnResult.setKey("success");
					returnResult.setValue("编辑成功");
				}else{
					returnResult.setKey("error");
					returnResult.setValue("名称不可重复");
				}
			}else{
				OperatorRefuselist  operatorRefuselist=operatorRefuselistRepository.findOne(id);
				operatorRefuselist.setCategoryName(refuseName);
				OperatorUser operatorUser=operatorUserRepository.findOne(userid);
				if(operatorUser!=null){
					operatorRefuselist.setCreateUser(operatorUser.getUsername());	
				}
				operatorRefuselist.setCreateDate(new Date());
				operatorRefuselistRepository.save(operatorRefuselist);
				returnResult.setKey("success");
				returnResult.setValue("编辑成功");
				
			}
		}else{
			returnResult.setKey("error");
			returnResult.setValue("名称不可为空");
		}
		
		return  returnResult;
	}
	//下架
	@Transactional
	public   ReturnResult   editShelves(String id,String refuseName,String userid){
		ReturnResult  returnResult=new ReturnResult();
		if(refuseName!=null&&!refuseName.equals("")){
			List<OperatorRefuselist> list= operatorRefuselistRepository.selectByShelvesCategoryName(refuseName);
			if(list.size()>0){
				if(list.get(0).getCategoryName().equals(refuseName)){
					returnResult.setKey("success");
					returnResult.setValue("编辑成功");
				}else{
					returnResult.setKey("error");
					returnResult.setValue("名称不可重复");
				}
			}else{
				OperatorRefuselist  operatorRefuselist=operatorRefuselistRepository.findOne(id);
				operatorRefuselist.setCategoryName(refuseName);
				OperatorUser operatorUser=operatorUserRepository.findOne(userid);
				if(operatorUser!=null){
					operatorRefuselist.setCreateUser(operatorUser.getUsername());	
				}
				operatorRefuselist.setCreateDate(new Date());
				operatorRefuselistRepository.save(operatorRefuselist);
				returnResult.setKey("success");
				returnResult.setValue("编辑成功");
				
			}
		}else{
			returnResult.setKey("error");
			returnResult.setValue("名称不可为空");
		}
		
		return  returnResult;
	}
	//编辑关闭原因
	@Transactional
	public   ReturnResult   editCloseOrder(String id,String refuseName,String userid){
		ReturnResult  returnResult=new ReturnResult();
		if(refuseName!=null&&!refuseName.equals("")){
			List<OperatorRefuselist> list= operatorRefuselistRepository.selectByOder(refuseName);
			if(list.size()>0){
				if(list.get(0).getCategoryName().equals(refuseName)){
					returnResult.setKey("success");
					returnResult.setValue("编辑成功");
				}else{
					returnResult.setKey("error");
					returnResult.setValue("名称不可重复");
				}
			}else{
				OperatorRefuselist  operatorRefuselist=operatorRefuselistRepository.findOne(id);
				operatorRefuselist.setCategoryName(refuseName);
				OperatorUser operatorUser=operatorUserRepository.findOne(userid);
				if(operatorUser!=null){
					operatorRefuselist.setCreateUser(operatorUser.getUsername());	
				}
				operatorRefuselist.setCreateDate(new Date());
				operatorRefuselistRepository.save(operatorRefuselist);
				returnResult.setKey("success");
				returnResult.setValue("编辑成功");
			}
		}else{
			returnResult.setKey("error");
			returnResult.setValue("名称不可为空");
		}
		return  returnResult;
	}
	
	@Transactional
	public ReturnResult deleterefuseList(String ids){
		    ReturnResult returnResult=new ReturnResult();
			String[] str=StringUtil.StrList(ids);
			List<OperatorRefuselist>  lists=new ArrayList<>();
			String id="";
			OperatorRefuselist operatorRefuselist=null;
			for (int i = 0; i < str.length; i++) {
				operatorRefuselist=new OperatorRefuselist();
				id=str[i];
				operatorRefuselist.setId(id);
				lists.add(operatorRefuselist);
			}
			operatorRefuselistRepository.delete(lists);
			returnResult.setKey("success");
			returnResult.setValue("删除成功");
		return returnResult;
	}
	@Transactional
	public ReturnResult deleteCommrefuse(String ids){
		ReturnResult returnResult=new ReturnResult();
		String[] str=StringUtil.StrList(ids);
		List<OperatorRefuselist>  lists=new ArrayList<>();
		String id="";
		OperatorRefuselist operatorRefuselist=null;
		for (int i = 0; i < str.length; i++) {
			operatorRefuselist=new OperatorRefuselist();
			id=str[i];
			operatorRefuselist.setId(id);
			lists.add(operatorRefuselist);
		}
		operatorRefuselistRepository.delete(lists);
		returnResult.setKey("success");
		returnResult.setValue("删除成功");
		return returnResult;
	}
	@Transactional
	public ReturnResult deleteCloseOrder(String ids){
		ReturnResult returnResult=new ReturnResult();
		String[] str=StringUtil.StrList(ids);
		List<OperatorRefuselist>  lists=new ArrayList<>();
		String id="";
		OperatorRefuselist operatorRefuselist=null;
		for (int i = 0; i < str.length; i++) {
			operatorRefuselist=new OperatorRefuselist();
			id=str[i];
			operatorRefuselist.setId(id);
			lists.add(operatorRefuselist);
		}
		operatorRefuselistRepository.delete(lists);
		returnResult.setKey("success");
		returnResult.setValue("删除成功");
		return returnResult;
	}
	
	@Transactional
	public ReturnResult deletereUnifuseList(String id){
		
		ReturnResult returnResult=new ReturnResult();
		OperatorRefuselist  operatorRefuselist=operatorRefuselistRepository.findOne(id);
		if(operatorRefuselist!=null){
			operatorRefuselistRepository.delete(id);
			returnResult.setKey("success");
			returnResult.setValue("删除成功");
		}else{
			returnResult.setKey("error");
			returnResult.setValue("删除失败");
		}
		return  returnResult;
	}
	
	public Page<OperatorRefuselist>  queryRefuseNforList(Integer page,Integer size,String refuseName){
		Specification<OperatorRefuselist> specification=new Specification<OperatorRefuselist>() {
			@Override
			public Predicate toPredicate(Root<OperatorRefuselist> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				if(StringUtils.isNotEmpty(refuseName)){
					lstPredicates.add(criteriaBuilder.like(root.get("categoryName"), "%" + refuseName+ "%"));//模糊查询

				}
					lstPredicates.add(criteriaBuilder.equal(root.get("attribution"), 1));//等于
				
				
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Sort sort = new Sort(Sort.Direction.DESC, "id");
		Pageable pageable = new PageRequest(page, size, sort);
		Page<OperatorRefuselist> list=operatorRefuselistRepository.findAll(specification, pageable); 
		return list;
	}
	public Page<OperatorRefuselist>  queryRefuseCommNforList(Integer page,Integer size,String refuseName){
		Specification<OperatorRefuselist> specification=new Specification<OperatorRefuselist>() {
			@Override
			public Predicate toPredicate(Root<OperatorRefuselist> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				if(StringUtils.isNotEmpty(refuseName)){
					lstPredicates.add(criteriaBuilder.like(root.get("categoryName"), "%" + refuseName+ "%"));//模糊查询

				}
					lstPredicates.add(criteriaBuilder.equal(root.get("attribution"), 2));//等于
				
				
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Sort sort = new Sort(Sort.Direction.DESC, "id");
		Pageable pageable = new PageRequest(page, size, sort);
		Page<OperatorRefuselist> list=operatorRefuselistRepository.findAll(specification, pageable); 
		return list;
	}
	public Page<OperatorRefuselist>  queryShelvesNforList(Integer page,Integer size,String refuseName){
		Specification<OperatorRefuselist> specification=new Specification<OperatorRefuselist>() {
			@Override
			public Predicate toPredicate(Root<OperatorRefuselist> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				if(StringUtils.isNotEmpty(refuseName)){
					lstPredicates.add(criteriaBuilder.like(root.get("categoryName"), "%" + refuseName+ "%"));//模糊查询

				}
					lstPredicates.add(criteriaBuilder.equal(root.get("attribution"), 4));//等于
				
				
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Sort sort = new Sort(Sort.Direction.DESC, "id");
		Pageable pageable = new PageRequest(page, size, sort);
		Page<OperatorRefuselist> list=operatorRefuselistRepository.findAll(specification, pageable); 
		return list;
	}
	public Page<OperatorRefuselist>  queryCloseOrderNforList(Integer page,Integer size,String refuseName){
		Specification<OperatorRefuselist> specification=new Specification<OperatorRefuselist>() {
			@Override
			public Predicate toPredicate(Root<OperatorRefuselist> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				if(StringUtils.isNotEmpty(refuseName)){
					lstPredicates.add(criteriaBuilder.like(root.get("categoryName"), "%" + refuseName+ "%"));//模糊查询

				}
					lstPredicates.add(criteriaBuilder.equal(root.get("attribution"), 3));//等于
				
				
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Sort sort = new Sort(Sort.Direction.DESC, "id");
		Pageable pageable = new PageRequest(page, size, sort);
		Page<OperatorRefuselist> list=operatorRefuselistRepository.findAll(specification, pageable); 
		return list;
	}
	
	public  List<OperatorRefuselist> closeOrderReasonList(){
		List<OperatorRefuselist>  list=	operatorRefuselistRepository.selectByOrderrefuse();
		if(list.size()>0){
			return  list;
		}
		return null;
	}
	
	
	
	
	

}
